package com.internetCafes.spms.common.module.sms;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

/**
 * 阿里大鱼短信
 */
public class SmsAlidayu {

    final static String product = "Dysmsapi";//短信API产品名称（短信产品名固定，无需修改）
    final static String domain = "dysmsapi.aliyuncs.com";//短信API产品域名（接口地址固定）
    final static String accessKeyId = "LTAIAXr51hJccRmD";
    final static String accessKeySecret = "jfx6th83qvNLbIedRUQHzXMizKEXdD";
    final static String MsgName = "虹合科技";//必填:短信签名-可在短信控制台中找到
    final static String verificationCodeTp= "SMS_142386989"; //必填:短信模板-可在短信控制台中找到
    final static String ThreeExpireTp= "SMS_190278249"; //必填:短信模板-可在短信控制台中找到
    final static String OneExpireTp= "SMS_190268375"; //必填:短信模板-可在短信控制台中找到
    final static String ExpireTp= "SMS_190278144"; //必填:短信模板-可在短信控制台中找到
    final static String EXPIRE_NOTICE_TP="SMS_213090192";


    private SmsAlidayu() {
    }


    /**
     * 验证码
     * @param mobile
     * @param code
     * @return
     */
    public static String sendVerificationCode(String mobile, String code){
        return sendTplShortMessage(mobile, verificationCodeTp, "{\"code\":\""+code+"\"}");
    }


    /**
     * 3个月到期
     * @param mobile
     * @param name
     * @param sevenDaysExpire
     * @param halfMonthExpire
     * @param threeMonthExpire
     * @return
     */
    public static String sendExpireNotice(String mobile, String name, Long sevenDaysExpire,Long halfMonthExpire,Long threeMonthExpire){
        return sendTplShortMessage(mobile, EXPIRE_NOTICE_TP,
                "{\"name\":\""+name+"\", \"num1\":\""+ sevenDaysExpire +"\", \"num2\":\""+ halfMonthExpire +"\", \"num3\":\""+ threeMonthExpire +"\"}");
    }

    /**
     *
     * @param mobile 用户手机号
     * @param verificationCodeTp 短信模板
     * @param templateParam 模板参数
     * @return
     */
    public static String sendTplShortMessage(String mobile, String verificationCodeTp, String templateParam){
        //设置超时时间-可自行调整
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret);
        try {
            DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain);
        } catch (ClientException e) {
            e.printStackTrace();
        }
        IAcsClient acsClient = new DefaultAcsClient(profile);
        //组装请求对象
        SendSmsRequest request = new SendSmsRequest();
        //使用post提交
        request.setMethod(MethodType.POST);
        request.setPhoneNumbers(mobile);
        request.setSignName(MsgName);
        request.setTemplateCode(verificationCodeTp);
        request.setTemplateParam(templateParam);
        SendSmsResponse sendSmsResponse = null;
        try {
            sendSmsResponse = acsClient.getAcsResponse(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
            if(sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
            return "OK";
        }
        return null;
    }

    public static void main(String[] args) {
        try {
            //String ss = sendVerificationCode("18621742606","589666");
            //String ss = sendExpireNotice("18621742606","杭州青橙科技有限公司", "5", "2020-05-15");
            //System.out.println(ss);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 提取编码
     */
    private static String getSendMsCode(Map resultMap) {
        Set keySet = resultMap.keySet();
        Iterator keyIte = keySet.iterator();
        while (keyIte.hasNext()) {
            String key = (String) keyIte.next();
            if (key.equals("code") || key.equals("err_code")) {
                if (null != resultMap.get(key) && resultMap.get(key) instanceof String) {
                    return (String) resultMap.get(key);
                } else if (null != resultMap.get(key) && resultMap.get(key) instanceof Integer) {
                    Integer val = (Integer) resultMap.get(key);
                    return val.toString();
                }

            }
            Object valObj = resultMap.get(key);
            if (valObj instanceof Map) {
                Map valMap = (Map) valObj;
                return getSendMsCode(valMap);
            }
        }
        return null;
    }
}
